<?php

require_once ('system\core\Model.php');
class schedulingmodel extends CI_Model {
	
	function __construct() {
		parent::__construct ();
		$this->load->library('session'); 
		$this->load->database();
	}
	function updatescheduling($array)
	{
		$sql ="insert into scheduling(`sid`,`date`,`leadid`,`staffid`) values(?,?,?,?)";
		$deletesql = "delete from scheduling where `date`=?";
		foreach($array as $row)
		{
			$query = $this->db->query($deletesql,array($row["date"]));
			$query = $this->db->query($sql,array(null,$row["date"],
				$row["leadid"],
				$row["staffid"])
				);
		}
	}
	function getlistscheduling($startdate=null,$enddate=null,$userid = -1)
	{
		if ($startdate == null && $enddate==null)
		{
			$query = $this->db->query('SELECT * FROM scheduling where (leadid = ? or staffid=? or -1=?) order by date desc'
				,array($userid,$userid,$userid));
		}
		else if ($startdate != null&&$startdate == null)
		{
			$query = $this->db->query('SELECT * FROM scheduling where (`date`>=?) and (leadid = ? or staffid=? or -1=?) order by date desc'
				,array($startdate,$userid,$userid,$userid));
		}
		else if ($startdate == null&&$startdate != null)
		{
			$query = $this->db->query('SELECT * FROM scheduling where (`date`<=?) and (leadid = ? or staffid=? or -1=?) order by date desc'
				,array($enddate,$userid,$userid,$userid));
		}
		else
		{
			$query = $this->db->query('SELECT * FROM scheduling where (`date`>=? and `date`<=?) and (leadid = ? or staffid=? or -1=?) order by date desc'
				,array($startdate,$enddate,$userid,$userid,$userid));
		}
		
		return $query->result_array();
	}
	
	function getscheduling($id)
	{
		$query = $this->db->query('SELECT * FROM scheduling where sid=? order by date ',array($id));
		foreach ($query->result_array() as $row)
		{
			//print_r($row);
			return $row;
		}
		return null;
	}
	function editscheduling($sid,$leadid,$staffid)
	{
		$query = $this->db->query('update scheduling set leadid=?,staffid=? where sid=?',array($leadid,$staffid,$sid));
		return $this->db->affected_rows();
	}
}

?>